package com.funny.live.web.controller.book;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.funny.live.common.core.controller.BaseController;
import com.funny.live.common.core.domain.AjaxResult;
import com.funny.live.common.core.page.TableDataInfo;
import com.funny.live.entity.Book;
import com.funny.live.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController extends BaseController {
    @Autowired
    private BookService bookService;

    /**
     * 查询列表（通过书名或者作者查询）
     */
    @GetMapping("/list")
    public TableDataInfo list(Book book){
        startPage();
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        List<Book> list = bookService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 新增
     */
    @PostMapping
    public AjaxResult add(@RequestBody Book book){
        if (book != null){
            book.setCreateTime(new Date());
        }
        return bookService.save(book)?AjaxResult.success(book):AjaxResult.error();
    }

    /**
     * 修改
     */
    @PutMapping
    public AjaxResult update(@RequestBody Book book){
        if (book != null){
            book.setUpdateTime(new Date());
        }
        return bookService.updateById(book)?AjaxResult.success(book):AjaxResult.error();
    }

}
